﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebsiteLab2.MasterPages
{
    public partial class WebForm3 : System.Web.UI.Page
    {

        private BUS.WebServiceInvoker invoker;
        private string[] allTypes = { "int", "bool", "string", "float" };

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                LoadComboBoxes();
                this.lbError.Text = string.Empty;                
            }
        }

        protected void LoadComboBoxes(){
            this.ddlFirstParam.DataSource = allTypes;
            this.ddlFirstParam.DataBind();

            this.ddlSecondParam.DataSource = allTypes;
            this.ddlSecondParam.DataBind();

            this.ddlThirdParam.DataSource = allTypes;
            this.ddlThirdParam.DataBind();

            this.ddlReturnValue.DataSource = allTypes;
            this.ddlReturnValue.DataBind();
        }


        protected void btnCallServices_Click(object sender, EventArgs e)
        {
            string wsURL = string.Empty;
            object[] param = null;
            object fParam = null;
            object sParam = null;
            object tParam = null;
            object result = null;

            // Load webservices 
            try
            {
                wsURL = this.txtLinkWebserivces.Text;
                if (String.IsNullOrEmpty(wsURL)) { throw new Exception("URL WS is empty !"); }
                this.invoker = new WebsiteLab2.BUS.WebServiceInvoker(new Uri(wsURL));
                
            }
            catch (Exception ex)
            {
                this.lbError.Text = String.Format("Unable to load services : {0} ", ex.Message);
                return;
            }

            if (this.invoker.AvailableServices.Count < 1)
            {
                this.lbError.Text = String.Format("Not exist services name ");
                return;
            }

            string servicesName = this.invoker.AvailableServices[0];

            // Method Name
            string methodName = txtMethodName.Text;

            // First Name
            string fName = this.txtFirstParam.Text;
            string fNameType = this.ddlFirstParam.Items[this.ddlFirstParam.SelectedIndex].Value;
            bool fNameChecked = this.cbFirstParam.Checked;

            // Second Name
            string sName = this.txtSecondParam.Text;
            string sNameType = this.ddlSecondParam.Items[this.ddlSecondParam.SelectedIndex].Value;
            bool sNameChecked = this.cbSecondParam.Checked;

            // Third Name
            string tName = this.txtThirdParam.Text;
            string tNameType = this.ddlThirdParam.Items[this.ddlThirdParam.SelectedIndex].Value;
            bool tNameChecked = this.cbThirdParam.Checked;

            // Return Type
            string rNameType = this.ddlReturnValue.Items[this.ddlReturnValue.SelectedIndex].Value;

            if (fNameChecked && sNameChecked && tNameChecked)
            {
                string fValue = this.txtFirstParam.Text;
                fParam = BUS.ProductBUS.createObject(fValue, fNameType);
                string sValue = this.txtSecondParam.Text;
                sParam = BUS.ProductBUS.createObject(sValue, sNameType);
                string tValue = this.txtThirdParam.Text;
                tParam = BUS.ProductBUS.createObject(tValue, tNameType);
                param = new object[] {fParam, sParam, tParam};     
            }
            else if (fNameChecked && sNameChecked){                
                string fValue = this.txtFirstParam.Text;
                fParam = BUS.ProductBUS.createObject(fValue, fNameType);
                string sValue = this.txtSecondParam.Text;
                sParam = BUS.ProductBUS.createObject(sValue, sNameType);
                param = new object[]{fParam, sParam};                
            }
            else if (fNameChecked && tNameChecked){
                string fValue = this.txtFirstParam.Text;
                fParam = BUS.ProductBUS.createObject(fValue, fNameType);
                string tValue = this.txtThirdParam.Text;
                tParam = BUS.ProductBUS.createObject(tValue, tNameType);
                param = new object[] { fParam, tParam };   
            }
            else if (sNameChecked && tNameChecked){
                string sValue = this.txtSecondParam.Text;
                sParam = BUS.ProductBUS.createObject(sValue, sNameType);
                string tValue = this.txtThirdParam.Text;
                tParam = BUS.ProductBUS.createObject(tValue, tNameType);
                param = new object[] {sParam, tParam};   
            }
            else if (fNameChecked){
                string fValue = this.txtFirstParam.Text;
                fParam = BUS.ProductBUS.createObject(fValue, fNameType);
                param = new object[] {fParam}; 
            }
            else if (sNameChecked){
                string sValue = this.txtSecondParam.Text;
                sParam = BUS.ProductBUS.createObject(sValue, sNameType);
                param = new object[] {sParam}; 
            }
            else if (tNameChecked){
                string tValue = this.txtThirdParam.Text;
                tParam = BUS.ProductBUS.createObject(tValue, tNameType);
                param = new object[] {tParam};
            }

            try
            {
                this.lbError.Text = string.Empty;

                if (param == null) { throw new Exception("None parameter is selected !"); }                
                
                switch (rNameType)
                {
                    case "int": result = this.invoker.InvokeMethod<int>(servicesName, methodName, param); break;
                    case "float": result = this.invoker.InvokeMethod<float>(servicesName, methodName, param); break;
                    case "bool": result = this.invoker.InvokeMethod<bool>(servicesName, methodName, param); break;
                    default : result = this.invoker.InvokeMethod<string>(servicesName, methodName, param); break;
                }
                
                this.txtReturnValue.Text = result.ToString();
            }
            catch (Exception ex)
            {
                this.lbError.Text = String.Format(" Error Invoking Method : {0} ", ex.Message);
                return;
            }
        }
    }
}
